package com.face.console.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.face.console.dao.RoleDtoMapper;
import com.face.console.dao.RoleMenuDtoMapper;
import com.face.console.pojo.dto.RoleDto;
import com.face.console.pojo.dto.RoleMenuDto;
import com.face.console.pojo.request.MenuIdRequest;
import com.face.console.pojo.request.RoleIdAndMenuIdsRequest;
import com.face.console.service.RoleService;
import com.face.console.tools.Error;
import com.face.console.tools.R;
import com.face.console.tools.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Liudehua
 * @explain
 * @date 2021/01/20 17:27
 */
@Service
public class RoleImpl implements RoleService {

    @Autowired
    private RoleMenuDtoMapper roleMenuDtoMapper;

    @Autowired
    private RoleDtoMapper roleDtoMapper;

    @Override
    public R saveRole(RoleDto param) {
        if (param == null) {
            return R.error(Error.参数为空异常);
        }

        if (param.getId() == null) {
            param.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
            roleDtoMapper.insertSelective(param);
        } else {
            roleDtoMapper.updateByPrimaryKeySelective(param);
        }
        return R.success();
    }

    @Override
    public R mangerMenu(RoleIdAndMenuIdsRequest menuIds) {

        //查询表中是否有该角色，有则更新数据，没有则新增数据；
        if (CollectionUtil.isNotEmpty(roleMenuDtoMapper.selectAllByRoleId(menuIds.getRoleId()))) {
            for (MenuIdRequest menuId : menuIds.getMenuIdRequests()) {
                RoleMenuDto entity = menuIdsToRole(menuIds);
                entity.setMenuId(menuId.getMenuId());
                roleMenuDtoMapper.updateByPrimaryKeySelective(entity);
            }
            //
        }else {
            for (MenuIdRequest menuId : menuIds.getMenuIdRequests()) {
                RoleMenuDto entity = menuIdsToRole(menuIds);
                entity.setMenuId(menuId.getMenuId());
                roleMenuDtoMapper.insertSelective(entity);
            }
        }
        return R.success();
    }

    public RoleMenuDto menuIdsToRole(RoleIdAndMenuIdsRequest menuIdsRequest) {
        RoleMenuDto entity = new RoleMenuDto();
        entity.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
        entity.setRoleId(menuIdsRequest.getRoleId());
        return entity;
    }

    /**
     * 更新 角色管理-菜单管理
     * @param menuIdsRequest
     * @return
     */
    public RoleMenuDto UpMenuIdsToRole(RoleIdAndMenuIdsRequest menuIdsRequest) {
        RoleMenuDto entity = new RoleMenuDto();
        entity.setRoleId(menuIdsRequest.getRoleId());
        return entity;
    }

    @Override
    public R queryRoleMenuById(List<MenuIdRequest> menuIds) {
        return null;
    }

    @Override
    public R selectAllMenuByRoleId(long roleId) {
        return R.success( roleMenuDtoMapper.selectAllByRoleId(roleId));
    }

    @Override
    public R delRoleById(Long roleId) {
        if (roleDtoMapper.deleteByPrimaryKey(roleId) > 0){
            roleMenuDtoMapper.delByRoleId(roleId);
            return R.success();
        }else{
            return R.error(Error.删除失败);
        }

    }
}
